package com.example.blog.service;

import com.example.blog.mapper.ArticleMapper;
import com.example.blog.model.ArticleInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Question ArticleService
 * @Date 2023/1/6 10:15
 * @Solution
 */
@Service
public class ArticleService {
    @Autowired
    ArticleMapper articleMapper;
    public List<ArticleInfo> getByUid(int uid,int state) {
        return articleMapper.getByUid(uid,state);
    }
    public ArticleInfo getById(int id) {
        return articleMapper.getById(id);
    }
    public int add(String title,String content,int uid,int state,String createtime,String updatetime) {
        return articleMapper.add(title,content,uid,state,createtime,updatetime);
    }
    public int del(int id) {
        return articleMapper.del(id);
    }
    public void rcountAdd(int id) {
        articleMapper.rcountAdd(id);
    }
    public void updateState(String title, String content,int state,int blogId) {
        //获取下该篇文章原本的state，检查是否已经发布，如果已经是发布过了的话，就不修改createtime，仅修改updatetime
        ArticleInfo articleInfo = articleMapper.getById(blogId);
        int oldState = articleInfo.getState();
        articleMapper.updateState(title,content,state,blogId,oldState);
    }
    public List<ArticleInfo> getAll(){
        return articleMapper.getAll();
    }
    public List<ArticleInfo> getList(int uid,int state,Integer pindex,Integer psize) {
        int offset = (pindex-1)*psize;
        return articleMapper.getList(uid,state,psize,offset);
    }
    public List<ArticleInfo> getAllList(int state,Integer pindex,Integer psize) {
        int offset = (pindex-1)*psize;
        return articleMapper.getAllList(state,psize,offset);
    }
}
